﻿using Library.Common.Common;
using Library.Core.Domain;
using Synthesize.BaseSet.Domain.Aggregation.Entity.Authority;
using Synthesize.BaseSet.Domain.Repository.Authority;

namespace Synthesize.BaseSet.Domain.Factory.Authority
{
    public class UserFactory
    {
        private readonly IUserRepository _userRepository;

        public UserFactory(IUserRepository userRepository)
        {
            _userRepository = userRepository;
        }

        public async System.Threading.Tasks.Task<UserInfoEntity> CreateAsync(string username)
        {
            var user = new UserInfoEntity
            {
                Id =GuidHelper.GetSecuentialGuid(),
                UserName = username,
                IsValid = true,
            };
            user.ResetPassword();
            if (await _userRepository.AnyAsync(x => x.UserName == user.UserName))
            {
                throw new DomainException("创建的登陆账号不能重复");
            }
            return user;
        }
    }
}
